package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.glory.brepda.R;
import com.glory.brepda.adapter.ChangeShiftsAdapter;
import com.glory.brepda.adapter.ChangeShiftsTestAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.ChangeShiftEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * FileName: ShiftExchangeTestActivity
 * Author: XJH
 * Date: 2021/6/24 15:58
 * Description:
 * History:
 */
public class ShiftExchangeTestActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etShift)
    FreeUI_ClearEditText etShift;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.etEqp)
    FreeUI_ClearEditText etEqp;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.list_tv5)
    TextView listTv5;
    @BindView(R.id.list_tv6)
    TextView listTv6;
    @BindView(R.id.list_tv7)
    TextView listTv7;
    @BindView(R.id.tvClear)
    TextView tvClear;
    @BindView(R.id.list_tv8)
    TextView listTv8;
    @BindView(R.id.list_btn)
    Button listBtn;
    @BindView(R.id.rvLotInfoList)
    RecyclerView rvLotInfoList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.etFinishNum)
    FreeUI_ClearEditText etFinishNum;
    @BindView(R.id.etSurplusNum)
    FreeUI_ClearEditText etSurplusNum;
    @BindView(R.id.etClassType)
    TextView etClassType;

    private ChangeShiftsTestAdapter mChangeShiftsAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_shift_exchange_test;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        listTv1.setText("序号");
        listTv2.setText("设备");
        listTv3.setText("批号");
        listTv4.setText("已测数量");
        listTv5.setText("重测数量");
        listTv6.setText("技术员");
        listTv7.setText("班次类型");
        listTv8.setText("备注");
        listTv5.setVisibility(View.VISIBLE);
        listTv6.setVisibility(View.VISIBLE);
        listTv7.setVisibility(View.VISIBLE);
        listTv8.setVisibility(View.VISIBLE);
        listBtn.setVisibility(View.VISIBLE);
        initRecyclerView(rvLotInfoList,this);
        tvBaseSubmit.setText("添加");
        mChangeShiftsAdapter = new ChangeShiftsTestAdapter(null);
        rvLotInfoList.setAdapter(mChangeShiftsAdapter);
    }

    @Override
    protected void initData() {

        initEdit2(etShift, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                    (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                        etClassType.setText(response.body().getResponse().getBody().getCLAZZ());
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(),"TESTCHANGESHIFT");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE","SELECT");
            map.addProperty("REFEROWNER",s);
//            NetUtils.requestMESPostNet("CPC.TESTCHANGESHIFT",this,map,callBack);
        });
    }


    @OnClick({R.id.tvBaseSubmit,R.id.list_btn,R.id.tvClear})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tvBaseSubmit:
                if (getText(etShift).isEmpty()){
                    showToast("请输入作业员",false);
                    return;
                }

                if (getText(etLot).isEmpty()){
                    showToast("请输入批次号",false);
                    return;
                }

                if (getText(etUser).isEmpty()){
                    showToast("请输入技术员",false);
                    return;
                }

                if (getText(etFinishNum).isEmpty()){
                    showToast("请输入已测数量",false);
                    return;
                }

                if (getText(etEqp).isEmpty()){
                    showToast("请扫描设备号",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                                    mChangeShiftsAdapter.addData(0,response.body().getResponse().getBody().getTESTCHANGESHIFTLINELIST());
                                    mChangeShiftsAdapter.notifyDataSetChanged();
                                    clearContent();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TESTCHANGESHIFT");
                            }
                        };

                        JsonObject map = new JsonObject();
                        List<ChangeShiftEntity.ChangeShift> data = new ArrayList<>();
                        map.addProperty("ACTIONTYPE","REFER");
                        ChangeShiftEntity.ChangeShift bean = new ChangeShiftEntity.ChangeShift();
//                bean.setOBJECTRRN("");
//                bean.setCLASSTYPE("");
                        bean.setOPERATORS(getText(etShift));//作业员
                        bean.setTECHNICIAN(getText(etUser));//技术员
                        bean.setMEASUREDQTY(getText(etFinishNum));//已测数量
                        bean.setRETESTQTY(getText(etSurplusNum));//重测数量
                        bean.setREMARKS(getText(etComment));//备注
                        bean.setLOTID(getText(etLot));//批次
                        bean.setCLASSTYPE(etClassType.getText().toString());//班别
                        bean.setEQUIPMENTID(getText(etEqp));//设备
                        data.add(bean);
                        map.add("TESTCHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(data)).getAsJsonArray());
//                        NetUtils.requestMESPostNet("CPC.TESTCHANGESHIFT",this,map,callBack);
                    }
                });
                break;
            case R.id.list_btn:
                if (mChangeShiftsAdapter.deleteData.size() <= 0) {
                    showToast("请勾选批次后，在进行删除操作",false);
                    return;
                }

                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ChangeShiftEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ChangeShiftEntity>>
                                (new TypeToken<BaseResponseBean<ChangeShiftEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                                    for (ChangeShiftEntity.ChangeShift data : mChangeShiftsAdapter.deleteData){
                                        Iterator<ChangeShiftEntity.ChangeShift> iterator = mChangeShiftsAdapter.getData().iterator();
                                        while (iterator.hasNext()){
                                            if (iterator.next().getLOTID().equals(data.getLOTID())) {
                                                iterator.remove();
                                            }
                                        }
                                    }

                                    mChangeShiftsAdapter.notifyDataSetChanged();
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ChangeShiftEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TESTCHANGESHIFT");
                            }
                        };

                        JsonObject map = new JsonObject();
                        List<ChangeShiftEntity.ChangeShift> data = new ArrayList<>();
                        map.addProperty("ACTIONTYPE","DELETE");
                        map.add("TESTCHANGESHIFTLINELIST",new JsonParser().parse(new Gson().toJson(mChangeShiftsAdapter.deleteData)).getAsJsonArray());
//                        NetUtils.requestMESPostNet("CPC.TESTCHANGESHIFT",this,map,callBack);
                    }
                },"是否确认删除");

                break;
            case R.id.tvClear:
                mChangeShiftsAdapter.getData().clear();
                mChangeShiftsAdapter.notifyDataSetChanged();
                clearContent();
                break;
        }


    }

    private void clearContent() {
        etShift.setText("");
        etUser.setText("");
        etFinishNum.setText("");
        etSurplusNum.setText("");
        etComment.setText("");
        etLot.setText("");
        etClassType.setText("");
    }
}
